/*
 * @Copyright: Copyright (c) 2030 吴周海
 * @Author: wzh200x@126.com
 * @Date: 2025-10-16 15:33:03
 * @LastEditors: wzh200x@126.com
 * @LastEditTime: 2025-10-22 14:31:01
 * @FilePath: \gogamewebserver\goGameWebServer\internal\platform\platformAlipay.go
 * @Description: 文件功能描述
 */
package platform

import (
	"context"
	"fmt"
	"goGameWebServer/internal/common/config"
	"goGameWebServer/internal/common/logger"

	"github.com/smartwalle/alipay/v3"
)

type PlatformAlipay struct {
	PlatformBase
}

func (p *PlatformAlipay) Login(code string, option *config.PlatformOption) (output map[string]interface{}, err error) {
	// 1. 通过code获取微信会话信息
	session, err := p.GetSession(option.AppId, option.AppPrivateKey, option.AppPublicKey, code)
	if err != nil {
		return output, err
	}
	output = make(map[string]interface{})
	output["open_id"] = session.OpenId
	output["session_key"] = session.AccessToken
	return output, nil
}

// 通过code获取支付宝会话信息
func (p *PlatformAlipay) GetSession(appid string, privateKey string, publicKey string, code string) (*alipay.SystemOauthTokenRsp, error) {
	client, err := alipay.New(appid, privateKey, true)
	if err != nil {
		return nil, err
	}
	// 换取授权访问令牌
	params := alipay.SystemOauthToken{
		GrantType: "authorization_code",
		Code:      code,
	}
	resp, err := client.SystemOauthToken(context.TODO(), params)
	if err != nil {
		return nil, err
	}

	if resp.Code != "" {
		return nil, fmt.Errorf("支付宝API错误: %s %s", resp.Code, resp.Msg)
	}

	return resp, nil
}

func (p *PlatformAlipay) CallThird(url string, datas map[string]string, option *config.PlatformOption) (re map[string]interface{}, err error) {
	client, err := alipay.New(option.AppId, option.AppPrivateKey, true)
	if err != nil {
		return nil, err
	}
	req := alipay.NewPayload(url)
	req.Verify = false
	// 遍历datas添加参数
	for k, v := range datas {
		req.AddBizField(k, v)
	}
	var result map[string]interface{}
	err = client.Request(context.Background(), req, &result)
	if err != nil {
		return nil, err
	}
	return result, nil
}

func (p *PlatformAlipay) Report(params map[string]interface{}, option *config.PlatformOption) (re map[string]interface{}, err error) {
	re = make(map[string]interface{})
	client, err := alipay.New(option.AppId, option.AppPrivateKey, true)
	if err != nil {
		return re, err
	}
	req := alipay.NewPayload("alipay.user.gamecenter.incrementgameaction.submit")
	req.Verify = false
	req.AddBizField("action_finish_date", params["action_finish_date"])
	req.AddBizField("open_id", params["open_id"])
	req.AddBizField("action_code", params["action_code"])
	req.AddBizField("action_finish_channel", params["action_finish_channel"])
	req.AddBizField("out_biz_no", params["out_biz_no"])
	var result map[string]interface{}
	err = client.Request(context.Background(), req, &result)
	if err != nil {
		return re, err
	}
	if result["code"] != "10000" {
		logger.LoggerIns().Errorf("支付宝上报失败: %v", result)
	} else {
		// 上报成功记录日志信息
		logger.LoggerIns().Infof("支付宝上报成功: open_id: %s, action_code: %s, action_finish_channel: %s, out_biz_no: %s, result: %v", params["open_id"],
			params["action_code"], params["action_finish_channel"], params["out_biz_no"], result)
	}

	re["code"] = result["code"]
	re["msg"] = result["msg"]
	re["result"] = result
	return re, err
}

func NewPlatformAlipay() *PlatformAlipay {
	return &PlatformAlipay{
		PlatformBase: PlatformBase{},
	}
}
